package com.ncd.disease.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ncd.common.annotation.Log;
import com.ncd.common.core.controller.BaseController;
import com.ncd.common.core.domain.AjaxResult;
import com.ncd.common.core.page.TableDataInfo;
import com.ncd.common.core.text.Convert;
import com.ncd.common.enums.BusinessType;
import com.ncd.common.utils.ShiroUtils;
import com.ncd.disease.domain.BloodInfo;
import com.ncd.disease.param.BloodParam;
import com.ncd.disease.service.IBloodInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 血液信息登记Controller
 * 
 * @author zhouxin
 * @date 2024-06-18
 */
@Controller
@RequestMapping("/disease/blood")
public class BloodInfoController extends BaseController
{
    private String prefix = "disease/blood";

    @Autowired
    private IBloodInfoService bloodInfoService;

    @RequiresPermissions("disease:info:view")
    @GetMapping()
    public String info()
    {
        return prefix + "/info";
    }

    /**
     * 查询血液信息登记列表
     */
    @RequiresPermissions("disease:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BloodParam param)
    {
        startPage();
        List<BloodInfo> list = bloodInfoService.selectBloodInfoList(param);
        return getDataTable(list);
    }

    /**
     * 新增血液信息登记
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 修改血液信息登记
     */
    @RequiresPermissions("disease:info:edit")
    @GetMapping("/edit/{biId}")
    public String edit(@PathVariable("biId") Long biId, ModelMap mmap)
    {
        BloodInfo bloodInfo = bloodInfoService.getById(biId);
        mmap.put("bloodInfo", bloodInfo);
        return prefix + "/add";
    }

    /**
     * 修改保存血液信息登记
     */
    @RequiresPermissions("disease:info:edit")
    @Log(title = "血液信息登记", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody BloodInfo bloodInfo)
    {
        if(ObjectUtil.isNull(bloodInfo.getBiId())){
            bloodInfo.setBiDate(new Date());
            bloodInfo.setBiUserName(ShiroUtils.getSysUser().getUserName());
        }
        bloodInfo.setBiUserId(ShiroUtils.getUserId());
        return toAjax(bloodInfoService.saveOrUpdate(bloodInfo));
    }

    /**
     * 删除血液信息登记
     */
    @RequiresPermissions("disease:info:remove")
    @Log(title = "血液信息登记", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bloodInfoService.removeByIds(Convert.toLongList(ids)));
    }
}
